package www.study.com;

//最长公共前缀 https://leetcode.cn/problems/longest-common-prefix/
public class code14 {
    public static void main(String[] args) {

    }

    class Solution {
        public String longestCommonPrefix(String[] strs) {
            int len = Integer.MAX_VALUE;
            for(int i = 0 ; i < strs.length ; i++){
                len = Math.min(len, strs[i].length());
            }
            for(int range = len ; range >= 1 ; range--){
                String preStr = strs[0].substring(0, range); //substring别拼错了
                boolean exit = true;
                for(int i = 1 ; i < strs.length ; i++){
                    if(strs[i].indexOf(preStr) != 0){ //indexOf由于是前缀，因此不仅仅是包含该字符串，字符串一定要在0位置
                        exit = false;
                        break;
                    }
                }
                if(exit) return preStr;
            }
            return "";
        }
    }
}
